/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package entities;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import scores.HibernateUtil;
import java.util.ArrayList;
import javax.faces.model.SelectItem;
import org.hibernate.Query;

/**
 *
 * @author Maciek
 */
public class Market {

    private long id;
    private double odds_home;
    private double odds_draw;
    private double odds_away;
    private String result;
    private specificMarket specificMarket;
    private Game game;
    private Session session;
    private boolean drawAvaliable;
    private SelectItem[] markets;


    public String dodaj() {
        try {
            SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
            setSession(sessionFactory.openSession());
            Transaction tx = getSession().beginTransaction();
            this.setResult("n");
            getSession().save(this);
            tx.commit();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            getSession().close();
        }
        return "ok";
    }

    /**
     * @return the id
     */
    public long getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(long id) {
        this.id = id;
    }

    /**
     * @return the odds_home
     */
    public double getOdds_home() {
        return odds_home;
    }

    /**
     * @param odds_home the odds_home to set
     */
    public void setOdds_home(double odds_home) {
        this.odds_home = odds_home;
    }

    /**
     * @return the odds_draw
     */
    public double getOdds_draw() {
        return odds_draw;
    }

    /**
     * @param odds_draw the odds_draw to set
     */
    public void setOdds_draw(double odds_draw) {
        this.odds_draw = odds_draw;
    }

    /**
     * @return the odds_away
     */
    public double getOdds_away() {
        return odds_away;
    }

    /**
     * @param odds_away the odds_away to set
     */
    public void setOdds_away(double odds_away) {
        this.odds_away = odds_away;
    }

    /**
     * @return the game
     */
    public Game getGame() {
        return game;
    }

    /**
     * @param game the game to set
     */
    public void setGame(Game game) {
        this.game = game;
    }

    /**
     * @return the session
     */
    public Session getSession() {
        return session;
    }

    /**
     * @param session the session to set
     */
    public void setSession(Session session) {
        this.session = session;
    }

    public boolean isDrawAvaliable(){
        return odds_draw>1;
    }

    /**
     * @return the specificMarket
     */
    public specificMarket getSpecificMarket() {
        return specificMarket;
    }

    /**
     * @param specificMarket the specificMarket to set
     */
    public void setSpecificMarket(specificMarket specificMarket) {
        this.specificMarket = specificMarket;
    }

    public SelectItem[] getMarkets() {
        ArrayList marketNames = new ArrayList();

        try {
            SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

            session = sessionFactory.openSession();
            Transaction tx = session.beginTransaction();
            Query q = session.createQuery("from specificMarket");
            marketNames = (ArrayList) q.list();
            tx.commit();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (session != null) {
                session.close();
            }
        }

        markets = new SelectItem[marketNames.size()];
        for (int i = 0; i < marketNames.size(); i++) {
            markets[i] = new SelectItem(marketNames.get(i), ((specificMarket) (marketNames.get(i))).getName());
        }
        return markets;
    }

    /**
     * @return the result
     */
    public String getResult() {
        return result;
    }

    /**
     * @param result the result to set
     */
    public void setResult(String result) {
        this.result = result;
    }
}
